一个 Ruby 脚本不仅仅是命令的序列;它是一个结构化的体系,从静态文件转变为运行中的进程。这一生命周期由 解释器 解析恰好三种表达式类型: 字面量 (固定值), 变量引用以及 方法调用。
1. 语法基础
Ruby 保留特定的关键字——记录在 表 22.3 (例如, alias, class, yield)——这些关键字构成了语言的骨架结构。它们不能用作标识符,以确保解析器能够区分逻辑与数据。
2. 执行门控
模块化设计的一个关键模式是 if __FILE__ == $0。这用于判断文件是入口点(主脚本)还是作为库被加载。通过利用 __FILE__ 和 __LINE__,程序可以在文件系统中保持自我认知。
3. 嵌入式数据
该 __END__ 标记充当物理尾部。解释器会忽略其后的一切内容,但可通过 DATA IO 对象提供数据,从而实现自包含的配置或模板。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following is NOT one of the three expression types Ruby evaluates?
Literals
Variable references
Macro definitions
Method invocations
✅ Correct!
Ruby strictly evaluates Literals, Variable references, and Method invocations. Macros are not a core expression type in standard Ruby.❌ Incorrect
Review the core concept: Ruby's interpreter handles Literals, Variables, and Methods.QUESTION 2
What is the purpose of the
if __FILE__ == $0 idiom?To check if the file is empty.
To differentiate between running a file directly vs. requiring it as a library.
To reset the object_id of the main thread.
To loop through the SCRIPT_LINES__ array.
✅ Correct!
This logic gate ensures code (like tests or demos) only runs when the script is the primary execution target.❌ Incorrect
It is about execution context: is this the main script or a dependency?QUESTION 3
Which constant provides access to the text placed after the
__END__ token?EOF
TRAILER
DATA
SCRIPT_LINES__
✅ Correct!
The DATA constant is an IO object pointing to the content after the __END__ marker.❌ Incorrect
Check the section on Embedded Data Segments; the answer is a four-letter constant.QUESTION 4
According to Table 22.3, which of these is a Reserved Word?
puts
yield
string
object_id
✅ Correct!
yield is a keyword used for blocks; puts and object_id are methods, and string is a class name.❌ Incorrect
Keywords like yield, alias, and elsif are reserved by the language core.QUESTION 5
What information do
RUBY_VERSION and RUBY_PLATFORM provide?The local variable scope count.
The environmental context of the execution environment.
The memory address of the TOPLEVEL_BINDING.
The number of lines in the current file.
✅ Correct!
These constants define the interpreter's version and the underlying hardware/OS architecture.❌ Incorrect
These are environmental 'magic' constants.The Self-Contained Utility Case
Applying Source Layout Principles
A developer needs to create a Ruby script that acts as both a standalone CLI tool and a reusable module. It must include default configuration values within itself and verify it is running on Ruby 3.0 or higher.
Q
1. How should the developer implement the version check and the 'main' execution logic?
Solution:
Use
Use
abort if RUBY_VERSION < '3.0' at the top. Wrap the CLI logic in if __FILE__ == $0 to prevent it from running when the file is required by other scripts.Q
2. Where should the default configuration be stored for maximum portability without external files?
Solution:
Place the configuration text after an
Place the configuration text after an
__END__ marker at the bottom of the script and read it using the DATA constant (e.g., YAML.load(DATA)).